<!--
 * @Author:
 * @Date: 2023-09-19 15:45:03
 * @LastEditors:
 * @LastEditTime: 2023-09-26 16:25:09
 * @FilePath: \immoka-admin\src\views\homePopup\popupList.vue
 * @Description:
 *
 * Copyright (c) 2023 by ${git_name} , All Rights Reserved.
-->
<template>
  <el-card class="m-8">
    <section slot="header" class="mb-4 flex items-center justify-between">
      <div>
        <el-button class="mr-4" type="primary" @click="handleAdd">新增</el-button>
      </div>
    </section>

    <el-table :data="tableList" align="center" border class="mt-4" stripe>
      <el-table-column :index="indexMethod" label="序号" type="index" width="120px"></el-table-column>
      <el-table-column label="名称" prop="title"/>
      <el-table-column label="跳转链接" prop="url"></el-table-column>
      <el-table-column label="标签" prop="tag_name"></el-table-column>
      <el-table-column label="排序" prop="sort"></el-table-column>
      <el-table-column label="状态" prop="status">
        <template slot-scope="{row}">
          <el-tag v-if="row.status === 0" type="warning" size="normal"  effect="dark">不显示</el-tag>
          <el-tag v-else type="success" size="normal"  effect="dark">显示</el-tag>
        </template>
      </el-table-column>

      <el-table-column align="center" label="操作" width="300">
        <template slot-scope="{row}">
          <el-button type="primary" @click="handleEdit(row)">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>

    <cus-pagination
      :limit.sync="listQuery.perPage"
      :page.sync="listQuery.page"
      :total="total"
      @pagination="initData"
    />

    <el-dialog
      :title="isEdit?'编辑':'新增'"
      :visible.sync="addDialog2"
      lock-scroll
      width="30%"
      @close="closeAddDialog2"
    >
      <el-form ref="form" :inline="false" :model="form" :rules="rules" label-width="150px" size="normal">
        <el-form-item label="提示名称" prop="title">
          <el-input type="text" v-model="form.title" placeholder="请输入提示名称"></el-input>
        </el-form-item>
        <el-form-item label="标签" prop="tag">
          <el-select v-model="form.tag" placeholder="请选择标签" clearable filterable>
            <el-option v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>

        </el-form-item>
        <el-form-item label="跳转地址" prop="url">
          <el-input type="textarea" rows="3" v-model="form.url" placeholder="请输入跳转地址" resize="none"></el-input>
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input v-model="form.sort" placeholder="请输入p排序" resize="none"></el-input>
        </el-form-item>
        <el-form-item label="提示状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio :label="0">不显示</el-radio>
            <el-radio :label="1">显示</el-radio>
          </el-radio-group>

        </el-form-item>
      </el-form>

      <section slot="footer">
        <el-button @click="addDialog2 = false">取消</el-button>
        <el-button type="primary" @click="onSubmit()">确定</el-button>
      </section>
    </el-dialog>

  </el-card>


</template>

<script>
import {
  adminPopList,
  adminPopSave,
  adminPopTagList
} from '@/api/homePopup'

export default {
  data() {
    return {
      options: [],
      selectData: '',
      menuList: [],
      tableList: [],
      user_code: '',
      nickname: '',
      dateList: undefined,
      listQuery: {
        type: 1,
        page: 1,
        perPage: 10
      },
      meta: {
        page: 1,
        perPage: 10

      },
      total: 0,
      /* 弹出框开关 */
      addDialog: false,
      addDialog2: false,

      rejectLoading: false,
      passLoading: false,
      id: '',
      // 拒绝原因
      reject_reason: '',
      form: {
        id: undefined,
        title: undefined,
        tag: undefined,
        url: undefined,
        sort: undefined,
        status: 1,
      },
      rules: {
        title: [
          { required: true, message: '请输入提示名称', trigger: 'blur' },
        ],
        tag: [
          { required: true, message: '请选择标签', trigger: 'blur' },
        ],
        url: [{ required: true, message: '请输入跳转地址', trigger: 'blur' }],
        sort: [{ required: true, message: '请输入排序', trigger: 'blur' }],
      },
      dataIds: '',
      editAllCount: '',
      editAllStatus: 1,
      editAllDialogVisible: false,
      isEdit: false,
      titleList: []
    }
  },
  created() {
    this.initData()
    this.getAdminPopTagList()
  },
  methods: {
    indexMethod(index) {
      return this.total - (this.listQuery.page - 1) * this.listQuery.perPage - index
    },
    getAdminPopTagList() {
      adminPopTagList().then(res => {
        if(res.code === 200) {
          for(let i in res.data) {
            this.options.push({
              value: i,
              label: res.data[i]
            })
          }
        }
      })
    },
    handleEdit(row) {
      this.form = JSON.parse(JSON.stringify(row))
      this.isEdit = true
      this.addDialog2 = true
    },
    handleAdd() {
      this.isEdit = false
      this.addDialog2 = true
    },
    async initData() {
      try {
        for (let key in this.listQuery) {
          this.listQuery[key] = this.listQuery[key] === '' ? undefined : this.listQuery[key]
        }
        const { data } = await adminPopList(this.listQuery)
        this.total = data.meta.total
        this.tableList = data.list

      } catch (e) {
        console.error(e)
      }
    },

    handleDelete(row) {
      this.$confirm('此操作不可恢复，是否继续删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        const { code, data } = await rechargeNoticeDel({
          id: row.id
        })
        if (code === 200) {

          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.initData()
        }

      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },

    onSubmit() {
      this.$refs['form'].validate(valid => {
        if (valid) {

          adminPopSave(this.form).then(res => {
            if (res.code === 200) {
              this.$message({
                type: 'success',
                message: '操作成功'
              })
              this.addDialog2 = false
              this.initData()
            }
          })
        }
      })
    },
    closeAddDialog2() {
      if (this.$refs['form']) {
        this.$refs['form'].clearValidate() // 清除整个表单的校验
        this.$refs['form'].resetFields() // 清除整个表单的校验
      }
      this.form = {
        id: undefined,
        title: undefined,
        tag: undefined,
        url: undefined,
        sort: undefined,
        status: 1,
      }
    }
  }
}
</script>

<style scoped>
.imageStyle {
  width: 150px;
}

.input {
  width: 200px;
}
</style>
